# 测试数据库配置脚本
# 用于验证开发环境和生产环境的数据库连接配置

Write-Host "=== 数据库配置测试 ===" -ForegroundColor Green

# 测试开发环境配置
Write-Host "\n1. 测试开发环境配置:" -ForegroundColor Yellow
$env:ASPNETCORE_ENVIRONMENT = "Development"
Write-Host "环境变量设置为: $env:ASPNETCORE_ENVIRONMENT"

# 启动应用并检查配置
Write-Host "启动开发环境服务..." -ForegroundColor Cyan
Start-Process -FilePath "dotnet" -ArgumentList "run" -WorkingDirectory "." -NoNewWindow -Wait -RedirectStandardOutput "dev-output.log" -RedirectStandardError "dev-error.log"

# 检查开发环境日志
if (Test-Path "dev-output.log") {
    Write-Host "\n开发环境启动日志:" -ForegroundColor Cyan
    Get-Content "dev-output.log" | Select-Object -Last 20
}

Write-Host "\n按任意键继续测试生产环境..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

# 测试生产环境配置
Write-Host "\n2. 测试生产环境配置:" -ForegroundColor Yellow
$env:ASPNETCORE_ENVIRONMENT = "Production"
Write-Host "环境变量设置为: $env:ASPNETCORE_ENVIRONMENT"

# 启动应用并检查配置
Write-Host "启动生产环境服务..." -ForegroundColor Cyan
Start-Process -FilePath "dotnet" -ArgumentList "run" -WorkingDirectory "." -NoNewWindow -Wait -RedirectStandardOutput "prod-output.log" -RedirectStandardError "prod-error.log"

# 检查生产环境日志
if (Test-Path "prod-output.log") {
    Write-Host "\n生产环境启动日志:" -ForegroundColor Cyan
    Get-Content "prod-output.log" | Select-Object -Last 20
}

Write-Host "\n=== 配置对比 ===" -ForegroundColor Green
Write-Host "开发环境数据库配置:" -ForegroundColor Yellow
Write-Host "- SQL Server: 10.1.200.169:1434" -ForegroundColor White
Write-Host "- Oracle: 10.1.210.6:1521" -ForegroundColor White

Write-Host "\n生产环境数据库配置:" -ForegroundColor Yellow
Write-Host "- SQL Server: 10.3.255.250:8660" -ForegroundColor White
Write-Host "- Oracle: 10.3.255.250:1621" -ForegroundColor White

Write-Host "\n测试完成!" -ForegroundColor Green

# 清理临时文件
Remove-Item "dev-output.log" -ErrorAction SilentlyContinue
Remove-Item "dev-error.log" -ErrorAction SilentlyContinue
Remove-Item "prod-output.log" -ErrorAction SilentlyContinue
Remove-Item "prod-error.log" -ErrorAction SilentlyContinue